<template>
  <el-menu
    class="demo custom-menu"
    :collapse-transition="false"  
    :collapse="isCollapse"        
    router                      
    text-color="#ffffff"        
    active-text-color="#ffd04b" 
    background-color="#304156"  
    :default-active="activeMenu"          
    :unique-opened="true"
    @open="handleOpen"          
    @close="handleClose"        
  >
    <div class="logo-container">
      <img src="../assets/logo.png" alt="LOGO" class="logo">
      <el-tooltip :content="$t('login.systemName')" placement="right" :disabled="!isTitleTruncated">
        <span class="system-name" v-show="!isCollapse">{{ $t('login.systemName') }}</span>
      </el-tooltip>
    </div>
    
    <!-- 递归组件：用于渲染菜单树 -->
    <template v-for="item in menuTree">
      <!-- 有子菜单的情况 -->
      <el-submenu v-if="item.children && item.children.length > 0" :index="item.id" :key="item.id">
        <template slot="title">
          <i :style="{ color: iconColor }" :class="item.icon || 'el-icon-menu'"></i>
          <el-tooltip :content="item.name" placement="right" :disabled="!isCollapse && !isTitleTruncated">
            <span class="menu-title">{{ item.name }}</span>
          </el-tooltip>
        </template>
        <el-menu-item-group>
          <template v-for="child in item.children">
            <!-- 递归渲染子菜单 -->
            <el-submenu v-if="child.children && child.children.length > 0" :index="child.id" :key="child.id">
              <template slot="title">
                <i :style="{ color: iconColor }" :class="child.icon || 'el-icon-menu'"></i>
                <el-tooltip :content="child.name" placement="right" :disabled="!isCollapse && !isTitleTruncated">
                  <span class="menu-title">{{ child.name }}</span>
                </el-tooltip>
              </template>
              <el-menu-item v-for="grandChild in child.children" :key="grandChild.id" :index="grandChild.url">
                <i :style="{ color: iconColor }" :class="grandChild.icon || 'el-icon-menu'"></i>
                <el-tooltip :content="grandChild.name" placement="right" :disabled="!isCollapse && !isTitleTruncated">
                  <span class="menu-title">{{ grandChild.name }}</span>
                </el-tooltip>
              </el-menu-item>
            </el-submenu>
            <!-- 没有子菜单的情况 -->
            <el-menu-item v-else :index="child.url" :key="child.id">
              <i :style="{ color: iconColor }" :class="child.icon || 'el-icon-menu'"></i>
              <el-tooltip :content="child.name" placement="right" :disabled="!isCollapse && !isTitleTruncated">
                <span class="menu-title">{{ child.name }}</span>
              </el-tooltip>
            </el-menu-item>
          </template>
        </el-menu-item-group>
      </el-submenu>
      <!-- 没有子菜单的根节点 -->
      <el-menu-item v-else :index="item.url" :key="item.id">
        <i :style="{ color: iconColor }" :class="item.icon || 'el-icon-menu'"></i>
        <el-tooltip :content="item.name" placement="right" :disabled="!isCollapse && !isTitleTruncated">
          <span class="menu-title">{{ item.name }}</span>
        </el-tooltip>
      </el-menu-item>
    </template>
  </el-menu>
</template>

<script>
import { mapGetters } from 'vuex'

export default {
  name: "Aside",
  data() {
    return {
      iconColor: '#c6e6e8', // 设置菜单项图标的颜色
      activeMenu: this.$route.path,
      isTitleTruncated: false // 标题是否被截断
    }
  },
  computed: {
    ...mapGetters(['getUserInfo']),
    // 将权限列表转换为树形结构
    menuTree() {
      const rights = this.getUserInfo?.rights || [];
      // 创建一个Map来存储所有节点，方便查找
      const nodeMap = new Map();
      // 存储根节点
      const rootNodes = [];

      // 第一次遍历，将所有节点存入Map
      rights.forEach(node => {
        // 创建新的节点对象，添加children属性
        const newNode = { ...node, children: [] };
        nodeMap.set(node.id, newNode);
      });
      
      // 第二次遍历，构建树形结构
      rights.forEach(node => {
        const currentNode = nodeMap.get(node.id);
        if (node.pid === 0) {
          // pid为0的是根节点
          rootNodes.push(currentNode);
        } else {
          // 将当前节点添加到父节点的children中
          const parentNode = nodeMap.get(node.pid);
          if (parentNode) {
            parentNode.children.push(currentNode);
          }
        }
      });


      // 按照id排序
      rootNodes.sort((a, b) => a.id - b.id);
      rootNodes.forEach(node => {
        if (node.children) {
          node.children.sort((a, b) => a.id - b.id);
        }
      });

      return rootNodes;
    }
  },
  props: {
    isCollapse: Boolean
  },
  mounted() {
    this.checkMenuTitlesOverflow();
    window.addEventListener('resize', this.checkMenuTitlesOverflow);
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.checkMenuTitlesOverflow);
  },
  watch: {
    '$i18n.locale'() {
      this.$nextTick(() => {
        this.checkMenuTitlesOverflow();
      });
    }
  },
  methods: {
    handleOpen(key, keyPath) {
      // 菜单展开时的处理方法
    },
    handleClose(key, keyPath) {
      // 菜单关闭时的处理方法
    },
    checkMenuTitlesOverflow() {
      this.$nextTick(() => {
        if (this.$i18n.locale === 'en') {
          this.isTitleTruncated = true;
        } else {
          const allMenuElements = document.querySelectorAll('.menu-title, .system-name');
          for (let el of allMenuElements) {
            if (el.scrollWidth > el.clientWidth) {
              this.isTitleTruncated = true;
              return;
            }
          }
          this.isTitleTruncated = false;
        }
      });
    }
  }
};
</script>

<style scoped>
.el-scrollbar {
  height: 100%; /* 设置滚动条的高度 */
}

.el-scrollbar__wrap {
  overflow-x: hidden; /* 隐藏水平滚动条 */
}

.el-menu {
  border-right-width: 0 !important; /* 移除右侧边框 */
}

.el-submenu__title i {
  color: #c6e6e8 !important; /* 设置子菜单标题图标的颜色 */
}

.custom-menu {
  height: 100%;
  overflow-x: hidden;
  min-height: 100%;
  width: 200px;
  box-shadow: 0 1px 4px rgba(0,21,41,.08);
  transition: width 0.3s;
}

.logo-container {
  display: flex;
  align-items: center;
  padding: 16px;
  color: #fff;
  transition: all 0.3s;
}

.logo {
  width: 32px;
  height: 32px;
  margin-right: 10px;
}

.system-name {
  font-size: 16px;
  font-weight: bold;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 150px;
}

.menu-item {
  transition: all 0.3s;
}

.menu-item:hover {
  background-color: #263445 !important;
}

.menu-icon {
  color: #ffffff;
  font-size: 18px;
  margin-right: 5px;
  width: 24px;
  text-align: center;
}

.menu-title {
  margin-left: 4px;
  transition: opacity 0.3s;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 140px;
  display: inline-block;
}

/* 为菜单标题添加工具提示效果 */
.el-menu-item, .el-submenu__title {
  overflow: hidden;
}

.el-menu--collapse {
  width: 64px;
}

.el-submenu__title:hover,
.el-menu-item:hover {
  background-color: #263445 !important;
}

.el-menu-item.is-active {
  background-color: #1890ff !important;
}

/* 移动端适配样式 */
@media screen and (max-width: 768px) {
  .logo-container {
    padding: 12px;
    justify-content: center;
  }
  
  .logo {
    width: 28px;
    height: 28px;
  }
  
  .system-name {
    font-size: 14px;
  }
  
  /* 确保移动端菜单项有足够的点击区域 */
  .el-menu-item, .el-submenu__title {
    height: 50px !important;
    line-height: 50px !important;
  }
  
  /* 移动端侧边栏宽度固定为200px */
  .custom-menu {
    width: 200px !important;
  }
  
  /* 确保在移动端菜单不会被折叠 */
  .el-menu:not(.el-menu--collapse) {
    width: 200px !important;
  }
  
  /* 移动端菜单文字适配 */
  .menu-title {
    max-width: 120px;
  }
}

::v-deep(.el-menu-item-group__title) {
    padding: 0 !important;
}

/* 添加悬停提示样式 */
.el-menu-item, .el-submenu__title {
  position: relative;
}

/* 适配不同语言时的最大宽度 */
[lang="en"] .menu-title {
  max-width: 130px;
  font-size: 0.95em;
}

[lang="en"] .system-name {
  max-width: 140px;
  font-size: 0.95em;
}
</style>